1829C - Mr Perfectly Fine - CodeForces Solution


bitmasks bitmasks greedy implementation

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define endl '\n'
#define nn 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define f0(n) for (int i = 0; i < n; i++)
#define f1(n) for (int i = 1; i < n; i++)
#define all(_a) _a.begin(), _a.end()
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x << " " << x << endl;
#else
#define debug(x)
#endif
#define rock_lee()                        \
    {                                     \
        ios_base::sync_with_stdio(false); \
        cin.tie(NULL);                    \
    }

void yoyochikichiki()
{
    rock_lee();
#ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
#endif
}
int32_t main()
{
    yoyochikichiki();
    int t;
    cin >> t;
    while (t--)
    {
        int sum = 0, flag = 0;
        int n;
        cin >> n;
        // debug(n);
        vector<int> b;
        vector<int> c;
        vector<int> d;
        for (int i = 0; i < n; i++)
        {
            int a;
            string s;
            cin >> a >> s;

            if (s == "10")
            {
                b.pb(a);
                // debug(b[0]);
            }
            else if (s == "01")
            {
                c.pb(a);
                // debug(c[0]);
            }
            else if (s == "11")
                d.pb(a);
        }
        if (b.size() == 0)
            b.pb(0);
        if (c.size() == 0)
            c.pb(0);
        if (d.size() == 0)
            d.pb(0);
        // for(int i=0;i<b.size();i++) cout<<b[i]<<" ";
        // cout << b[0];

        // cout << endl;
        sort(all(b));
        sort(all(c));
        sort(all(d));
        // debug(b[0]);
        // debug(c[0]);
        // debug(d[0]);
        int x = b[0] + c[0];
     
        if (d[0] == 0)
        {
            if (b[0] != 0 && c[0] != 0)
                cout << x << endl;
            else
                cout << -1 << endl;
        }
        else
        {
            if (b[0] == 0 || c[0] == 0)
                cout << d[0] << endl;
            else
            {
                if (x <= d[0])
                    cout << x << endl;
                else if (x > d[0])
                    cout << d[0] << endl;
                else
                    cout << -1 << endl;
            }
        }
    }
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST